<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\captcha\Captcha;

class Activity extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(){
        $list=db('gs_activity')->select();
        $this->assign('list',$list);
        return  $this->fetch();
    }
    //验证码
    public function add(){
        if( Request()->isPost() ){
            $inputdata = input();
            $data["title"] = $inputdata["title"];
            $data["activity"] = $inputdata["activity"];
            $data["img"] = $inputdata["img"];
            $data["sort"] = $inputdata["sort"];
            $data["status"] = $inputdata["status"];
            $res = db( "gs_activity" )->insert( $data );
            if( $res ){
                $this->success( "添加成功", url( "activity/index" ) );
            }else{
                $this->error( "添加失败" );
            }
        }else{
            return $this->fetch();
        }
    }
    public function upload(){
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move('public/upload/');
        if ($info) {
            $path = '/public/upload/'.$info->getSaveName();
            return json(['code'=>0,'msg'=>'上传成功！','data'=>$path]);
        }else{
            return json(['code'=>1,'msg'=>'上传失败！','data'=>'']);
        }
    }
    public function update(){
        $where=[];
        $id=input('id');
        $data = db('gs_activity')->where('id',$id)->find();
        $this->assign('data',$data);
        if(request()->isPost()) {
            $inputdata = input();
            $where['title'] = $inputdata['title'];
            $where['activity'] = $inputdata['activity'];
            $where['sort'] = $inputdata['sort'];
            $where['img'] = $inputdata['img'];
            $where['status'] = $inputdata['status'];
            unset($inputdata['file']);
            $res = db( "gs_activity" )->where('id',$inputdata['id'])->update( $inputdata );
            if( $res ){
                $this->success( "修改成功",url('activity/index') );
            }else{
                $this->error( '修改失败' );
            }
        }return $this->fetch();
    }
    public function delete($id){
        $id = input('id');
        $res=db('gs_activity')->where('id',$id)->delete();
        if($res) {
            $this->success('删除成功','activity/index');
        } else {
            $this->error('删除失败');
        }
    }
    //状态开启关闭
    public function state(){
        $status = input("status");
        $id = input("id");
        $where = array();
        $where["id"] = $id;
        $rst = db("gs_activity")->update(["status"=>$status]);
        if($rst){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
}
